package Limit.ETC;

import java.lang.reflect.Constructor;
import java.util.Random;
import java.util.logging.Logger;
import java.util.logging.Level;

import l1j.server.server.GeneralThreadPool;
import l1j.server.server.IdFactory;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.L1World;
import l1j.server.server.model.L1Location;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.templates.L1Npc;

public class EternalTower {
	
	private static Logger _log = Logger.getLogger(EternalTower.class.getName());
	
	private static Random _random = new Random();
	
	private static final int[][] _crack = {
		{ 32563, 32863, 5188},
		{ 32610, 32749, 5188},
		{ 32721, 32800, 5188}
	};

	private static EternalTower _instance;
	
	public static EternalTower getStart() {
		if (_instance == null) {
			_instance = new EternalTower();
		}
		return _instance;
	}
	
	private EternalTower() {
		spawnCrack();
	}

	private void spawnCrack() {
		L1Location crack = null;
		crack = new L1Location(32805, 32877, 613);
		createCrack(crack.getX(), crack.getY(), (short) crack.getMapId());
	}
	
	private void createCrack(int x, int y, short mapId) {
		try {
			L1Npc l1npc = NpcTable.getInstance().getTemplate(909024);

			if (l1npc == null) {
				return;
			}

			String s = l1npc.getImpl();
			Constructor<?> constructor = Class.forName("l1j.server.server.model.Instance." + s + "Instance").getConstructors()[0];
			Object aobj[] = { l1npc };
			L1NpcInstance npc = (L1NpcInstance) constructor.newInstance(aobj);

			npc.setId(IdFactory.getInstance().nextId());
			npc.setX(x);
			npc.setY(y);
			npc.setMap(mapId);
			npc.setHomeX(npc.getX());
			npc.setHomeY(npc.getY());
			npc.setHeading(0);

			L1World.getInstance().storeObject(npc);
			L1World.getInstance().addVisibleObject(npc);

			Teleport teleport = new Teleport(npc);
			GeneralThreadPool.getInstance().execute(teleport);
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
	}

	class Teleport implements Runnable {
		private L1NpcInstance _npc = null;

		public Teleport(L1NpcInstance npc) {
			_npc = npc;
		}

		public void run() {
			try {
				Thread.sleep(1000);
				for (;;) {
					if (_npc._destroyed) {
						return;
					}

					for (L1Object obj : L1World.getInstance().getVisiblePoint(_npc.getLocation(), 1)) {
						if (obj instanceof L1PcInstance) {
							int rnd1 = _random.nextInt(3);
							L1PcInstance target = (L1PcInstance) obj;
							L1Location tmp_loc = new L1Location(_crack[rnd1][0], _crack[rnd1][1], (short)_crack[rnd1][2]);
							L1Location rnd_loc = tmp_loc.randomLocation(1, 5, false);
							L1Teleport.teleport(target, rnd_loc.getX(), rnd_loc.getY(), (short) rnd_loc.getMapId(), target.getHeading(), true);
						}
					}
					Thread.sleep(1000);
				}
			} catch (Exception e) {
				_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			}
		}
	}
}